const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    companyName:'',
    bankArray: [],
    bankIndex:0,
    bankRadio:'',
    bankName:'',
    bankNameShow:false,
    enterpriseName:'',
    bankCard:'',
    confirmBankCard:'',
    typeText:'',
    againId:'',
    depositName:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

    if(options.typeText){
      this.setData({
        typeText: options.typeText
      })
    }else {
      this.reAuthInfo()
    }
    if(options.againId){
      this.setData({
        againId:options.againId
      })
    }
    // 可绑定的银行列表
    this.bindList();
  },

  // reAuthInfo
  reAuthInfo:function(){
    var _this = this;
    wx.request({
      url: app.globalData.baseUrl + 'auth/reAuthInfo',
      data: {
        token: app.globalData.token
      },
      success(res) {
        if (res.data.code == 1000) {
          _this.setData({
            typeText: res.data.data.realnameAuth.realname
          })
        } else {
          wx.showToast({
            title: res.data.message,
            icon: 'none',
            duration: 2000
          })
        }
      }
    })
  },


  selectBank(){
    this.setData({
      bankNameShow: true,
    });
  },
  onClose(){
    this.setData({
      bankName:this.data.bankRadio,
      bankNameShow: false,
    });
  },

  bankRadioChange(event){
    this.setData({
      bankRadio: event.detail,
    });
  },

  onbankClick(event){
    const { name } = event.currentTarget.dataset;
    this.setData({
      bankRadio: name,
    });
    setTimeout(()=>{
      this.onClose()
    },500)

  },

  // 可绑定的银行列表
  bindList:function(){
    var _this = this;
    wx.showLoading({
      title: '加载中',
    })
    wx.request({
      url: app.globalData.baseUrl + 'auth/getBankAuth',
      data: {
        token: app.globalData.token
      },
      success(res) {
        wx.hideLoading()
        console.log(res)
        if (res.data.code == 1000) {
          _this.setData({
            bankArray: res.data.data
          })
          console.log(res.data.data,'res.data.data2222')
        } else {
          wx.showToast({
            title: res.data.message,
            icon: 'none',
            duration: 2000
          })
        }
      }
    })
  },
  // 选择银行
  bankPickerChange:function(e){
    this.setData({
      bankIndex: e.detail.value
    })
  },
  // 开户行
  bankNameInp:function(e){
    this.setData({
      bankName: e.detail.value
    })
  },
  // 企业名称
  enterpriseNameInp: function (e) {
    this.setData({
      enterpriseName: e.detail.value
    })
  },
  // 银行卡号
  bankCardInp: function (e) {
    this.setData({
      bankCard: e.detail.value
    })
  },
  // 确认银行卡号
  confirmBankCardInp: function (e) {
    this.setData({
      confirmBankCard: e.detail.value
    })
  },
  // 提交
  submitBtn:function(){
    var { bankName, depositName, enterpriseName, bankCard, confirmBankCard,againId } = this.data
    var _this = this;
    wx.showLoading({
      title: '加载中',
    })
    wx.request({
      url: app.globalData.baseUrl + 'auth/bankAuth',
      data: {
        depositName: depositName,
        bankname:bankName,
        bankAccount: bankCard,
        confirmBankAccount: confirmBankCard,
        token: app.globalData.token,
        auth_id:againId
      },
      method: "POST",
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success(res) {
        wx.hideLoading()
        console.log(res)
        if (res.data.code == 1000) {
          wx.showToast({
            title: '提交成功',
            icon: 'success',
            duration: 2000
          })
          setTimeout(function(){
            wx.navigateBack({
              delta: 1
            })
          },2000)
        } else {
          wx.showToast({
            title: res.data.message,
            icon: 'none',
            duration: 2000
          })
        }
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})
